/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header$
 * $Id$
 * $Author$
 * $Date$
 * $Revision$
 */
package com.elancom.framework.web.ui;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import com.elancom.framework.commons.json.serializer.JSONSerializer;
import com.elancom.framework.commons.json.serializer.NameFilter;
import com.elancom.framework.commons.json.serializer.PropertyFilter;
import com.elancom.framework.commons.json.serializer.SerializeConfig;
import com.elancom.framework.commons.json.serializer.SerializerFeature;
import com.elancom.framework.commons.json.serializer.ValueFilter;

/**
 * @author elancom
 * @date 2012-7-26
 */
public class JsonModel implements Model, JModel {
	private PropertyFilter propertyFilter;

	private ValueFilter valueFilter;

	private NameFilter nameFilter;

	private Object object;

	private DateFormat dateFormat;

	public static String DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";

	private List<SerializerFeature> features = null;

	protected void config(SerializerFeature feature) {
		if (features == null) {
			features = new ArrayList<>(6);
		}
		features.add(feature);
	}

	protected SerializeConfig getSerializeConfig() {
		return SerializeConfig.getGlobalInstance();
	}

	protected DateFormat getDateFormat() {
		if (dateFormat == null) {
			dateFormat = new SimpleDateFormat(DEFFAULT_DATE_FORMAT);
		}

		return dateFormat;
	}

	public JsonModel setFeature(SerializerFeature feature) {
		if (this.features == null) {
			this.features = new ArrayList<>(3);
		}
		this.features.add(feature);
		return this;
	}

	protected List<SerializerFeature> getFeatures() {
		return this.features;
	}

	@Override
	public final String getJson() {
		final JSONSerializer serializer = new JSONSerializer(getSerializeConfig());

		if (this.features != null) {
			for (final com.elancom.framework.commons.json.serializer.SerializerFeature feature : this.features) {
				serializer.config(feature, true);
			}
		}

		if (propertyFilter != null) {
			serializer.getPropertyFilters().add(propertyFilter);
		}

		if (nameFilter != null) {
			serializer.getNameFilters().add(nameFilter);
		}

		if (valueFilter != null) {
			serializer.getValueFilters().add(valueFilter);
		}

		serializer.setDateFormat(getDateFormat());
		serializer.write(object);

		return serializer.toString();
	}

	/**
	 * @return the nameFilter
	 */
	public NameFilter getNameFilter() {
		return nameFilter;
	}

	@Override
	public Object getObject() {
		return object;
	}

	/**
	 * @return the propertyFilter
	 */
	public PropertyFilter getPropertyFilter() {
		return propertyFilter;
	}

	/**
	 * @return the valueFilter
	 */
	public ValueFilter getValueFilter() {
		return valueFilter;
	}

	/**
	 * @param nameFilter
	 *            the nameFilter to set
	 */
	public void setNameFilter(NameFilter nameFilter) {
		this.nameFilter = nameFilter;
	}

	@Override
	public void setObject(Object object) {
		this.object = object;
	}

	/**
	 * @param propertyFilter
	 *            the propertyFilter to set
	 */
	public void setPropertyFilter(PropertyFilter propertyFilter) {
		this.propertyFilter = propertyFilter;
	}

	/**
	 * @param valueFilter
	 *            the valueFilter to set
	 */
	public void setValueFilter(ValueFilter valueFilter) {
		this.valueFilter = valueFilter;
	}

	public static void main(String[] args) {
		JsonModel jsonModel = new JsonModel();

		jsonModel.setObject("肚子好饿");

		System.out.println(jsonModel.getJson());
	}

}
